/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmijcoltrane;

/**
 *
 * @author Felipe Gutierrez
 */


import java.util.ArrayList;
import java.util.Stack;

import net.sf.jColtrane.annotations.methods.BeforeElement;
import net.sf.jColtrane.annotations.methods.EndElement;
import net.sf.jColtrane.annotations.methods.StartElement;
import net.sf.jColtrane.handler.ContextVariables;
import net.sf.jColtrane.handler.SAXEvent;


import xmibase.XmiAttribute;
import xmibase.XmiGeneralization;
import xmibase.XmiPackage;

public class XmiGetGeneralization {
	public XmiPackage rootPackage;
	public Stack elements;
	public boolean debug = false;
	//public ArrayList<XMIGeneralization> generalizations;

	public XmiGetGeneralization(XmiPackage packages, Stack elements) {

		this.elements = elements;
		this.rootPackage = packages;

	}
	public void printDebug(String msg) {
		if (debug) {
			System.out.println(msg);
		}
	}
	/**
	 * start a Generalization element of the Package
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:Namespace.ownedElement")
	@StartElement(tag = "UML:Generalization")
	public void startGeneralizationPackage(ContextVariables contextVariables) {
		printDebug("start generalizable");

		SAXEvent event = contextVariables.getLastEvent();

		XmiGeneralization xmiGeneralization = new XmiGeneralization();

		xmiGeneralization.setId(event.getAtributesHolder().getValue("xmi.id"));

		this.elements.push(xmiGeneralization);

		contextVariables.getGeneralUseMap().put(xmiGeneralization.getId(),
				xmiGeneralization);

	}

	/**
	 * end of the Generalization element of the Package
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:Namespace.ownedElement")
	@EndElement(tag = "UML:Generalization")
	public void endGeneralizationPackage(ContextVariables contextVariables) {

		SAXEvent event = contextVariables.getLastEvent();

		XmiGeneralization xmiGeneralization = (XmiGeneralization) this.elements.peek();

		this.rootPackage.addGeneralization(xmiGeneralization);

		this.elements.pop();

		System.out.println("end generalization");

	}

	@BeforeElement(elementDeep = 1, tag = "UML:Generalization.parent")
	@StartElement(tag = "UML:Class")
	public void startGeneralizationChildElement(ContextVariables contextVariables) {

		SAXEvent event = contextVariables.getLastEvent();

		XmiGeneralization xmiGeneralization = (XmiGeneralization)this.elements.peek();

		xmiGeneralization.setIdParent(event.getAtributesHolder().getValue(
				"xmi.idref"));

	}

	/**
	 * start element UML:ClassType at UML:Attribute to get the xmi.idref, it is
	 * used to get attributes type
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML2:TypedElement.type")
	@StartElement(tag = "UML:Class")
	public void startTypeClassAttribute(ContextVariables contextVariables) {

		// System.out.println(contextVariables.getCurrentBranch());
		SAXEvent event = contextVariables.getLastEvent();

		Object obj = this.elements.peek();

		if (obj instanceof XmiAttribute) {
			XmiAttribute tempAtt = (XmiAttribute) obj;
			tempAtt.setIdDataType(event.getAtributesHolder().getValue(
					"xmi.idref"));
		}

	}

}
